package cn.zjyj.module.zjyj.controller.admin.riskapplyrecord;

import cn.zjyj.module.zjyj.controller.admin.riskassessmentdetail.vo.RiskAssessmentDetailRespVO;
import cn.zjyj.module.zjyj.dal.dataobject.riskassessmentdetail.RiskAssessmentDetailDO;
import cn.zjyj.module.zjyj.dal.dataobject.riskidentify.RiskIdentifyDO;
import cn.zjyj.module.zjyj.dal.mysql.riskassessmentdetail.RiskAssessmentDetailMapper;
import cn.zjyj.module.zjyj.dal.mysql.riskidentify.RiskIdentifyMapper;
import cn.zjyj.module.zjyj.service.risklist.RiskListService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import java.util.*;

import cn.zjyj.framework.common.common.pojo.PageResult;
import cn.zjyj.framework.common.common.pojo.CommonResult;
import cn.zjyj.framework.common.common.util.object.BeanUtils;
import static cn.zjyj.framework.common.common.pojo.CommonResult.success;

import cn.zjyj.module.zjyj.controller.admin.riskapplyrecord.vo.*;
import cn.zjyj.module.zjyj.dal.dataobject.riskapplyrecord.RiskAssessmentApplyRecordDO;
import cn.zjyj.module.zjyj.service.riskapplyrecord.RiskAssessmentApplyRecordService;

@Tag(name = "管理后台 - 风险申请记录")
@RestController
@RequestMapping("/zjyj/risk-apply-record")
@Validated
public class RiskAssessmentApplyRecordController {

    @Resource
    private RiskAssessmentApplyRecordService riskAssessmentApplyRecordService;

    @Resource
    RiskAssessmentDetailMapper riskAssessmentDetailMapper;

    @Resource
    RiskListService riskListService;

    @Resource
    RiskIdentifyMapper riskIdentifyMapper;


    @PostMapping("/create")
    @Operation(summary = "创建风险申请记录")
    @PreAuthorize("@ss.hasPermission('zjyj:risk-apply-record:create')")
    public CommonResult<Long> createRiskApplyRecord(@Valid @RequestBody RiskAssessmentApplyRecordSaveReqVO createReqVO) {
        return success(riskAssessmentApplyRecordService.createRiskApplyRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新风险申请记录")
    @PreAuthorize("@ss.hasPermission('zjyj:risk-apply-record:update')")
    public CommonResult<Boolean> updateRiskApplyRecord(@Valid @RequestBody RiskAssessmentApplyRecordSaveReqVO updateReqVO) {
        riskAssessmentApplyRecordService.updateRiskApplyRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除风险申请记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('zjyj:risk-apply-record:delete')")
    public CommonResult<Boolean> deleteRiskApplyRecord(@RequestParam("id") Long id) {
        riskAssessmentApplyRecordService.deleteRiskApplyRecord(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除风险申请记录")
                @PreAuthorize("@ss.hasPermission('zjyj:risk-apply-record:delete')")
    public CommonResult<Boolean> deleteRiskApplyRecordList(@RequestParam("ids") List<Long> ids) {
        riskAssessmentApplyRecordService.deleteRiskApplyRecordListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得风险申请记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('zjyj:risk-apply-record:query')")
    public CommonResult<RiskAssessmentApplyRecordRespVO> getRiskApplyRecord(@RequestParam("id") Long id) {
        RiskAssessmentApplyRecordDO riskApplyRecord = riskAssessmentApplyRecordService.getRiskApplyRecord(id);
        RiskAssessmentApplyRecordRespVO bean = BeanUtils.toBean(riskApplyRecord, RiskAssessmentApplyRecordRespVO.class);
        List<RiskAssessmentDetailDO> detailDOS = riskAssessmentDetailMapper.selectList(RiskAssessmentDetailDO::getApplyRecordId, id);
        RiskIdentifyDO identifyDO = riskIdentifyMapper.selectById(riskApplyRecord.getIdentifyId());
        String riskName = riskListService.getRiskName(identifyDO.getRiskType(), identifyDO.getRiskTypeInfoId());
        bean.setRiskTypeInstanceId(riskListService.getInstanceSize(identifyDO.getRiskType(), identifyDO.getRiskTypeInfoId()));

        bean.setRiskType(identifyDO.getRiskType());
        bean.setRiskTypeInfoId(identifyDO.getRiskTypeInfoId());
        bean.setRiskInfoName(riskName);
        bean.setDetails(BeanUtils.toBean(detailDOS, RiskAssessmentDetailRespVO.class));
        return success(bean);
    }

    @GetMapping("/page")
    @Operation(summary = "获得风险申请记录分页")
    @PreAuthorize("@ss.hasPermission('zjyj:risk-apply-record:query')")
    public CommonResult<PageResult<RiskAssessmentApplyRecordRespVO>> getRiskApplyRecordPage(@Valid RiskAssessmentApplyRecordPageReqVO pageReqVO) {
        PageResult<RiskAssessmentApplyRecordRespVO> pageResult = riskAssessmentApplyRecordService.getRiskApplyRecordPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RiskAssessmentApplyRecordRespVO.class));
    }



}